﻿//*** Guidance generated code ***//

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;

using Survey.Common;
using Survey.Web.Controls;

namespace Survey.Web
{
    /// <summary>
    ///     Appending any links, scripts etc. to HTML.HEAD area must be done using the
    ///     Head.Controls.Add() operation. No tags like &lt;%= %&gt; are allowed in runat="server" tag,
    ///     otherwise it cannot be accessed via Head reference.
    ///     Basic fixing for masterpages.
    /// </summary>
    public abstract class LayoutBase : RazorControl<IViewModel>, IPublishingReporter
    {
        #region members
        protected const string Delimiter = " - ";
        protected const string IEBrowser = "IE";
        protected const string IEMeta8 = "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\">";
        protected const string IEMeta9 = "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=100\" >";

        #endregion members

        protected virtual HtmlString FixMetaForIE
        {
            get
            {
                var sb = new StringBuilder();
                try
                {
                    int version;
                    if (Context.Request.Browser.Browser.Equals(IEBrowser, StringComparison.Ordinal)
                        && int.TryParse(Context.Request.Browser.Version.Split('.')[0], out version)
                        && version > 6)
                    {
                        sb.Append(version == 8 ? IEMeta8 : IEMeta9);
                    }
                }
                catch (Exception ex)
                {
                    this.PublishFatal("User Browser not recognized", ex);
                }
                return new HtmlString(sb.ToString());
            }
        }
        
        /// <summary>
        ///     Page Title based on the Web or Entity Model
        /// </summary>
        protected virtual string HeadTitle
        {
            get
            {
                var headTitle = new StringBuilder(GetLocalized(Str.Common.ApplicationName));

                headTitle.Append(Delimiter + GetLocalized(Model.MasterModel.ControllerName));

                if (Model.MasterModel.CurrentAction.IsNotEmpty())
                {
                    headTitle.Append(Delimiter + GetLocalized(Model.MasterModel.CurrentAction));
                }

                var entityModel = Model as IEntityModel<IPersistentObject, IFilter>;

                if (entityModel.Is() && entityModel.Item.Is())
                {
                    headTitle.Append(Delimiter + entityModel.Item.ToDisplay());
                }

                return headTitle.ToString();
            }
        }

        #region ResizeCSS
        protected virtual string BodyPosition { get { return GetPositionFromCookie() + "px"; } }
        protected virtual string MenuPosition { get { return (GetPositionFromCookie() - 11) + "px"; } }
        protected virtual string UserPosition { get { return (GetPositionFromCookie() - 27) + "px"; } }
        protected virtual string MovePosition { get { return (GetPositionFromCookie() - 3) + "px"; } }
        protected virtual int GetPositionFromCookie()
        {
            int position = 0;
            var tab = Context.Request.Cookies["resizeBar_position_left"];
            if (tab.Is() && tab.Value.IsNotEmpty())
            {
                if (!tab.Value.TryParse(out position))
                {
                    position = 188;
                }
            }
            if (position < 100 || position > 450)
            {
                position = 188;
            }
            return position;
        }
        #endregion ResizeCSS
    }
}